package com.dokimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dokimall.product.entity.AttrEntity;
import com.dokimall.product.service.AttrService;
import com.dokimall.product.service.CategoryService;
import com.dokimall.product.vo.AttrGroupVo;
import com.dokimall.product.vo.AttrRelDelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dokimall.product.entity.AttrGroupEntity;
import com.dokimall.product.service.AttrGroupService;
import com.dokimall.common.utils.PageUtils;
import com.dokimall.common.utils.R;



/**
 * 属性分组
 *
 * @author mori
 * @email 550144898@qq.com
 * @date 2021-09-26 15:02:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    /**
     * {catelogId}/withattr
     * 获取分类下所有分组&关联属性
     * */
    @GetMapping("{catelogId}/withattr")
    public R catelogIdWithAttr(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupVo> vos = attrGroupService.getAttrByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    /**
     * attr/relation
     * 添加属性与分组关联关系
     * */
    @PostMapping("attr/relation")
    public R addAttrRelation(@RequestBody List<AttrRelDelVo> vos){
        attrGroupService.saveBatch(vos);
        return R.ok();
    }

    /**
     * {attrgroupId}/noattr/relation
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId")Long attrgroupId){
        PageUtils page = attrService.noattrRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * {attrgroupId}/attr/relation
     * 获取指定分组关联的所有属性
     * */
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
        List<AttrEntity> attrs =  attrGroupService.getAttrRelation(attrgroupId);
        return R.ok().put("data",attrs);
    }

    /**
     * attr/relation/delete
     * 删除属性与分组的关联关系
     * */
    @PostMapping("attr/relation/delete")
    public R relationDelete(@RequestBody AttrRelDelVo[] attrRelDelVo){
        attrGroupService.attrRelationDelete(attrRelDelVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
